package com.zonhar.autofeed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonhar.autofeed.dao.DistConfigDao;
import com.zonhar.autofeed.entities.entity.DistConfigEntity;
import com.zonhar.autofeed.service.DistConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DistConfigServiceImpl extends ServiceImpl<DistConfigDao, DistConfigEntity> implements DistConfigService {
    @Override
    public Map<String, Object> getDistConfigMap() {
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_group","params");
        Map<String,Object> configMap =new HashMap<>();
        List<DistConfigEntity> distConfigEntities = this.list(queryWrapper);
        configMap.put("distGroup",distConfigEntities.get(0).getDistGroup());
        for (DistConfigEntity distConfigEntity : distConfigEntities) {
            if("true".equals(distConfigEntity.getDistValue()) || "false".equals(distConfigEntity.getDistValue())){
                configMap.put(distConfigEntity.getDistName(),"true".equals(distConfigEntity.getDistValue()));
            }else {
                configMap.put(distConfigEntity.getDistName(),distConfigEntity.getDistValue());
            }

        }
        return configMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByList(List<DistConfigEntity> configEntityList) {
        for (DistConfigEntity distConfigEntity : configEntityList) {
            QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("dist_name",distConfigEntity.getDistName());
            DistConfigEntity configEntity = this.getOne(queryWrapper);
            configEntity.setDistValue(distConfigEntity.getDistValue());
            this.updateById(configEntity);
        }
    }

    List<String> strings = Arrays.asList("cron","cronSg","prop","cronKg","propKg","","hcCron","propHc");

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByMap(Map<String, Object> params) {
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_group",params.get("distGroup").toString());
        List<DistConfigEntity> distConfigEntities = this.list(queryWrapper);
        for (DistConfigEntity distConfigEntity : distConfigEntities) {
            if(strings.contains(distConfigEntity.getDistName())){
                Boolean value = (Boolean) params.get(distConfigEntity.getDistName());
                distConfigEntity.setDistValue(value ? "true" : "false");
            }else {
                String value = (String) params.get(distConfigEntity.getDistName());
                distConfigEntity.setDistValue(value);
            }

            this.updateById(distConfigEntity);
        }
    }

    @Override
    public Map<String, Object> getSensorListByDistGroupName(String distGroupName) {
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_group",distGroupName);
        List<DistConfigEntity> distConfigEntities = this.list(queryWrapper);
        Map<String,Object> configMap =new HashMap<>();
        configMap.put("distGroup",distConfigEntities.get(0).getDistGroup());
        for (DistConfigEntity distConfigEntity : distConfigEntities) {
            if("true".equals(distConfigEntity.getDistValue()) || "false".equals(distConfigEntity.getDistValue())){
                configMap.put(distConfigEntity.getDistName(),"true".equals(distConfigEntity.getDistValue()));
            }else {
                configMap.put(distConfigEntity.getDistName(),distConfigEntity.getDistValue());
            }

        }
        return configMap;
    }
}
